package com.example.myapplication.module1.activity

import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.DividerItemDecoration
import com.example.myapplication.databinding.ActivityWorkNoticeBinding
import com.example.myapplication.entity.WorkNoticeBean
import com.example.myapplication.entity.WorkNoticeDetailBean
import com.example.myapplication.module1.adapter.WorkNoticeDetailAdapter
import com.example.myapplication.module1.presenter.WorkNoticeDetailPresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.BroadcastpromptXXRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.SpUtils
import com.example.mylibrary.utils.UiTools
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class WorkNoticeDetailActivity :
    BaseMvpActivity<WorkNoticeDetailActivity, WorkNoticeDetailPresenter, ActivityWorkNoticeBinding>() {
    var loadPopup: LoadingPopupView? = null
    var workNoticeBean: WorkNoticeBean? = null
    var soapEnvelop = SoapEnvelop()
    var soapRequestBody = SoapRequestBody()
    var broadcastpromptxx = BroadcastpromptXXRequest()
    var curpage = 1
    var pagesize = 20

    private val workNoticeDetailAdapter: WorkNoticeDetailAdapter by lazy {
        WorkNoticeDetailAdapter(this)
    }

    override fun getBinding(): ActivityWorkNoticeBinding {
        return ActivityWorkNoticeBinding.inflate(layoutInflater)
    }

    override fun getPresenter(): WorkNoticeDetailPresenter = WorkNoticeDetailPresenter()

    override fun initData() {
        loadPopup = builder.asLoading("加载中...")
        workNoticeBean = bundleExtra?.getParcelable<WorkNoticeBean>("workNotice")
        viewBinding.clToolbar.setTitle(workNoticeBean?.typeName)

        viewBinding.list.rvList.addItemDecoration(
            DividerItemDecoration(
                this,
                LinearLayout.VERTICAL
            )
        )
        viewBinding.list.rvList.adapter = workNoticeDetailAdapter

        workNoticeDetail()
    }

    // 获取工作提醒
    private fun workNoticeDetail() {
        if (curpage == 1) {
            tempWorkNotice.clear()
        }
        broadcastpromptxx.opid = SpUtils.get("opId", "") as String
        broadcastpromptxx.dbname = SpUtils.get("dbname", "") as String
        broadcastpromptxx.typecode = workNoticeBean?.typeCode!!
        broadcastpromptxx.curpage = "$curpage"
        broadcastpromptxx.pagesize = "$pagesize"
        soapRequestBody.value = broadcastpromptxx
        soapEnvelop.body = soapRequestBody
        mPresenter?.getWorkNotice(soapEnvelop)
    }

    override fun initView() {
        viewBinding.list.srlRefresh.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                curpage = 1
                workNoticeDetail()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                curpage += 1
                workNoticeDetail()
            }
        })

        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {

            }
        }
    }

    val tempWorkNotice = mutableListOf<WorkNoticeDetailBean>()

    fun setWorkNoticeDetail(result: String) {

        val workNoticeDetailBeans =
            GsonUtil.fromJson<MutableList<WorkNoticeDetailBean>>(
                result,
                object : TypeToken<MutableList<WorkNoticeDetailBean>>() {}.type
            )
        if (workNoticeDetailBeans.isNullOrEmpty()) {
            viewBinding.list.srlRefresh.setEnableLoadMore(false)
            UiTools.showToast("没有更多内容了")
        } else {
            viewBinding.list.srlRefresh.setEnableLoadMore(true)
            tempWorkNotice.addAll(workNoticeDetailBeans)
            workNoticeDetailAdapter.workNoticeDetailBeans = tempWorkNotice
        }
        if (tempWorkNotice.isNotEmpty()) {
            viewBinding.noData.clNoData.visibility = View.GONE
        } else {
            viewBinding.noData.clNoData.visibility = View.VISIBLE
        }


    }

    fun finishStatus() {
        loadPopup?.smartDismiss()
        if (viewBinding.list.srlRefresh.isRefreshing || viewBinding.list.srlRefresh.isLoading) {
            viewBinding.list.srlRefresh?.finishRefresh()
            viewBinding.list.srlRefresh?.finishLoadMore()
        }
    }

    // 初始化时刷新和加载状态
    fun startStatus() {
        if (!viewBinding.list.srlRefresh.isRefreshing && !viewBinding.list.srlRefresh.isLoading) {
            loadPopup?.show()
        }
    }
}
